package com.singzi.leetcode.N900;

public class T0807 {
    public static void main(String[] args) {
        Solution0807 solution0807 = new Solution0807();
        int [][]grid = {{3,0,8,4},{2,4,5,7},{9,2,6,3},{0,3,1,0}};
        System.out.println(solution0807.maxIncreaseKeepingSkyline(grid));
    }
}
class Solution0807 {
    public int maxIncreaseKeepingSkyline(int[][] grid) {

        int sum = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                sum += getRemain(grid, i, j);

            }
        }
        return sum;

    }
    public int getRemain(int[][] grid, int row, int col){

        int maxRow = grid[row][0];
        for (int i = 1; i < grid.length; i++) {
            if(grid[row][i] > maxRow) maxRow = grid[row][i];
        }
        int minCol = grid[0][col];
        for (int i = 1; i < grid.length; i++) {
            if(grid[i][col] > minCol) minCol = grid[i][col];
        }

        return Math.max(Math.min(maxRow,minCol), grid[row][col])-grid[row][col];
    }

}
